import 'package:flutter/material.dart';

import '../SecondPage.dart';
import 'HomePage.dart';
import 'ProfilePage.dart';
import 'SearchPage.dart';
class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  String result = '';  // 用于保存从 SecondPage 返回的结果

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Navigator 示例'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('从第二页返回的结果: $result'),
            ElevatedButton(
              onPressed: () async {
                // 使用 Navigator.push 进行页面跳转并接收返回值
                final returnedData = await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SecondPage(data1: '来自主页的参数'),
                  ),
                );
                // 接收从 SecondPage 返回的值
                setState(() {
                  result = returnedData ?? '';  // 如果返回值为空，使用空字符串
                });
              },
              child: Text('跳转到第二页'),
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavExample(),
    );
  }
}

class BottomNavExample extends StatefulWidget {
  @override
  _BottomNavExampleState createState() => _BottomNavExampleState();
}

class _BottomNavExampleState extends State<BottomNavExample> {
  int _selectedIndex = 0;

  // 定义不同的页面
  static List<Widget> _pages = <Widget>[
    HomePage(),
    SearchPage(),
    ProfilePage(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(title: Text('底部导航示例')),
      body: _pages[_selectedIndex],  // 根据索引展示不同页面
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.search), label: '搜索'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }
}

